{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "882d215b-88d8-4b5e-ae7a-0e3f6bb53bad",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "hide"
    ]
   },
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "sns.set_theme(style=\"whitegrid\")\n",
    "diamonds = sns.load_dataset(\"diamonds\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "9b8b892e-a96f-46e8-9c5e-8749783608d8",
   "metadata": {
    "editable": true,
    "raw_mimetype": "",
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "Draw a single horizontal plot, assigning the data directly to the coordinate variable:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "391e1162-b438-4486-9a08-60686ee8e96a",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "sns.boxenplot(x=diamonds[\"price\"])"
   ]
  },
  {
   "cell_type": "raw",
   "id": "b0c5a469-c709-4333-a8bc-b2cb34f366aa",
   "metadata": {
    "editable": true,
    "raw_mimetype": "",
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "Group by a categorical variable, referencing columns in a datafame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e30fec18-f127-40a3-bfaf-f71324dd60ec",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "sns.boxenplot(data=diamonds, x=\"price\", y=\"clarity\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "70fe999a-bea5-4b0a-a1a3-474b6696d1be",
   "metadata": {
    "editable": true,
    "raw_mimetype": "",
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "Group by another variable, representing it by the color of the boxes. By default, each boxen plot will be \"dodged\" so that they don't overlap; you can also add a small gap between them:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eed3239c-57b7-4d76-9fdc-be99257047fd",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "large_diamond = diamonds[\"carat\"].gt(1).rename(\"large_diamond\")\n",
    "sns.boxenplot(data=diamonds, x=\"price\", y=\"clarity\", hue=large_diamond, gap=.2)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "36030c1c-047b-4f7b-b366-91188b41680e",
   "metadata": {
    "editable": true,
    "raw_mimetype": "",
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "The default rule for choosing each box width represents the percentile covered by the box. Alternatively, you can reduce each box width by a linear factor:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d0c1aa43-5e8a-486c-bd6d-3c29d6d23138",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "sns.boxenplot(data=diamonds, x=\"price\", y=\"clarity\", width_method=\"linear\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "062a9fc2-9cbe-4e40-af8c-3fd35f785cd5",
   "metadata": {
    "editable": true,
    "raw_mimetype": "",
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "The `width` parameter itself, on the other hand, determines the width of the largest box:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4100a460-fe27-42b7-bbaf-4430a1c1359f",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "sns.boxenplot(data=diamonds, x=\"price\", y=\"clarity\", width=.5)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "407874a8-1202-4bcc-9f65-59e1fed29e07",
   "metadata": {
    "editable": true,
    "raw_mimetype": "",
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "There are several different approaches for choosing the number of boxes to draw, including a rule based on the confidence level of the percentie estimate:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1aead6a3-6f12-47d3-b472-a39c61867963",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "sns.boxenplot(data=diamonds, x=\"price\", y=\"clarity\", k_depth=\"trustworthy\", trust_alpha=0.01)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "71212196-d60e-4682-8dcb-0289956be152",
   "metadata": {
    "editable": true,
    "raw_mimetype": "",
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "The `linecolor` and `linewidth` parameters control the outlines of the boxes, while the `line_kws` parameter controls the line representing the median and the `flier_kws` parameter controls the appearance of the outliers:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd103426-a99f-476b-ae29-a11d52958cdb",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "sns.boxenplot(\n",
    "    data=diamonds, x=\"price\", y=\"clarity\",\n",
    "    linewidth=.5, linecolor=\".7\",\n",
    "    line_kws=dict(linewidth=1.5, color=\"#cde\"),\n",
    "    flier_kws=dict(facecolor=\".7\", linewidth=.5),\n",
    ")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "16f1c534-3316-4752-ae12-f65dee9275cb",
   "metadata": {
    "editable": true,
    "raw_mimetype": "",
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "It is also possible to draw unfilled boxes. With unfilled boxes, all elements will be drawn as line art and follow `hue`, when used:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab6aef09-5bbe-4c01-b6ba-05446982d775",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "sns.boxenplot(data=diamonds, x=\"price\", y=\"clarity\", hue=\"clarity\", fill=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e059b944-ea59-408d-87bb-4ce65074dab5",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py310",
   "language": "python",
   "name": "py310"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
